package com.init.controller;



import com.init.common.BaseResponse;
import com.init.common.ErrorCode;
import com.init.common.ResultUtils;
import com.init.exception.BusinessException;

import com.init.model.entity.Admin;
import com.init.model.entity.Graders;
import com.init.model.vo.LoginUserVO;
import com.init.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 登录注册
 * @author Atanycosts
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class LoginController {

    @Resource
    private LoginService loginService;



    /**
     * 管理员登录
     */
    @PostMapping("/login/admin/wobuzhidao")
    public BaseResponse<LoginUserVO> login(@RequestBody Admin admin) {

        LoginUserVO login = loginService.login(admin);
        if (login == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "账号或密码错误");
        }
        return ResultUtils.success(login);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login/graders")
    public BaseResponse<LoginUserVO> login(@RequestBody Graders graders) {

        LoginUserVO login = loginService.login(graders);
        if (login == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "账号或密码错误");
        }
        return ResultUtils.success(login);
    }


    /**
     * 管理员注册
     */
    @PostMapping("/register/admin/wobuzhidao")
    public BaseResponse<String> register(@RequestBody Admin admin) {

        boolean register = loginService.register(admin);
        if (!register) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "注册失败");
        }
        return ResultUtils.success("注册成功");
    }

    /**
     * 用户注册
     */
    @PostMapping("/register/graders")
    public BaseResponse<String> register(@RequestBody Graders graders) {

        Boolean register = loginService.register(graders);
        if (register == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "注册失败");
        }
        return ResultUtils.success("注册成功");
    }


    /**
     * 退出
     */
    @GetMapping("/loginOut")
    public BaseResponse<String> logout() {
        // 已排查
        loginService.logout();

        return ResultUtils.success("已退出");
    }

}
